package com.example.administrator.pandanotes;

import android.content.Intent;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.litepal.crud.DataSupport;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
/**
 * Created by 于博 on 2018-07-12.
 * 书籍类添加界面
 * 有添加按钮，点击添加书籍类
 */
public class BookClassAddActivity extends AppCompatActivity {
    //变量***************************************************************************
    private Button btnAdd = null;
    private EditText eTClassName = null;
    private User theUser = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_class_add);
        //初始化及绑定********************************************************************
        btnAdd = (Button) findViewById(R.id.btnBookClassAdd); //书籍类添加按钮
        eTClassName = (EditText) findViewById(R.id.eTClassName);//书籍类名
        //得到当前登录用户
        try {
            FileReader r = new FileReader(Environment.getExternalStorageDirectory() + "/pandanotescookies.txt");
            BufferedReader reader = new BufferedReader(r);
            String id = reader.readLine();
            Log.d("UserName:",id);
            if(id != null && (!id.equals(""))){
                List<User> users = DataSupport.where("user_id = ?",id).find(User.class);
                theUser = users.get(0);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //函数及事件****************************************************************
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//添加按钮点击事件，进行书籍类的添加
                //获得值************************************************************
                String ClassName = eTClassName.getText().toString();
                List<BookClass> BookClass = DataSupport.where("class_name=?",ClassName).find(BookClass.class);//找到书籍类中所填
                //异常处理
                if(ClassName.length()<1||ClassName.length()>16)//书籍类名审核
                {
                    Toast.makeText(BookClassAddActivity.this,"书籍类名为1-16位",Toast.LENGTH_SHORT).show();
                }
                else if (BookClass.size()>0)//找到同主键的
                {
                    Toast.makeText(BookClassAddActivity.this,"已有此书籍类",Toast.LENGTH_SHORT).show();
                }
                else
                {
                    BookClass bookClass = new BookClass(ClassName,R.drawable.bookclass);
                    bookClass.setClass_name(ClassName);//设置类名
                    bookClass.setBook_class_imageId(R.drawable.bookclass);//设置图片
                    theUser.getBookClasses().add(bookClass);//添加到当前用户
                    Boolean b = bookClass.save();  //保存
                    Log.d("添加书类：",bookClass.getClass_name()+bookClass.getBook_class_imageId());
                    if(b) {
                        Toast.makeText(BookClassAddActivity.this,"添加成功",Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(BookClassAddActivity.this,BookClassActivity.class);
                        startActivity(intent);
                    }
                    else Toast.makeText(BookClassAddActivity.this,"添加失败",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    //返回键调用函数，进行上一页*************************************************************
    @Override
    public void onBackPressed()
    {
        this.finish(); //关闭当前Activity
    }
}
